package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Set;
@Component
@RequiredArgsConstructor
public class ClearCartListener {
    private final ICartService cartService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue",durable = "true"),
            exchange = @Exchange(name = "trade.topic"),
            key = "order.create"
    ))
    // 传用户信息设置到UserContext中
    public void listenCreateOrder(Message message, Set<Long> itemIds){
        //这里message别导错包
        Long userId = message.getMessageProperties().getHeader("user_INFO");
        UserContext.setUser(userId);
        cartService.removeByItemIds(itemIds);
    }
}
